import path from 'path'
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import eslintPlugin from 'vite-plugin-eslint'
import terser from '@rollup/plugin-terser'
import Components from 'unplugin-vue-components/vite'
import { AntDesignVueResolver } from 'unplugin-vue-components/resolvers'
import VueSetupExtend from 'vite-plugin-vue-setup-extend'
// 变量前缀
const envPrefix = 'IFRAME_'

const resolvePath = filePath => {
    return path.resolve(__dirname, filePath)
}

export default defineConfig(() => {
    return {
        base: '/',
        envPrefix: envPrefix,
        resolve: {
            alias: {
                '/@': resolvePath('./src')
            },
            extensions: ['.js', '.json', '.vue', '.png', 'css', 'less']
        },
        esbuild: {
            drop: ['console', 'debugger']
        },
        server: {
            headers: {
                'Access-Control-Allow-Origin': '*'
            },
            port: 8888,
            proxy: {
                // '/api': {
                //     target: 'https://inverter.itzhuowen.cn/',
                //     changeOrigin: true
                // }
                '/api': {
                    target: 'http://localhost:9095',
                    changeOrigin: true,
                    rewrite: path => path.replace('/api', '')
                }
            }
        },
        build: {
            minify: 'esbuild',
            rollupOptions: {
                output: {
                    manualChunks: {
                        vue: ['vue'],
                        expand: ['vue-router', 'vue-dompurify-html', 'pinia'],
                        icon: ['@ant-design/icons-vue'],
                        dayjs: ['dayjs']
                    }
                },
                plugins: [
                    terser({
                        maxWorkers: 4
                    })
                ]
            }
        },
        plugins: [
            vue(),
            eslintPlugin({
                include: ['src/**/*.js', 'src/**/*.vue', 'src/*.js', 'src/*.vue']
            }),
            VueSetupExtend(),
            Components({
                resolvers: [
                    AntDesignVueResolver({
                        dirs: ['src/components'], // 组件所在目录
                        extensions: ['vue'], // 组件文件扩展名
                        deep: true, // 是否递归查找组件
                        importStyle: false // css in js
                    })
                ]
            })
        ]
    }
})
